package com.aj.service;

import com.aj.Interface.CheckGroupService;
import com.aj.dao.CheckgroupDao;
import com.aj.pojo.CheckGroup;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass =CheckGroupService.class )
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckgroupDao checkgroupDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage,queryPageBean.getPageSize() );
         Page<CheckGroup> page = checkgroupDao.findpage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public CheckGroup findGroupByid(Integer id) {
        CheckGroup groupByid = checkgroupDao.findGroupByid(id);
        return groupByid;
    }

    /**
     * 根据checkGroupId查询检查项的ids
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> ids = checkgroupDao.findCheckItemIdsByCheckGroupId(id);
        return ids;
    }


    /**
     * 修改检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
            checkgroupDao.deleteCheckGroupAndCheckItem(checkGroup.getId());
            checkgroupDao.update(checkGroup);
            setCheckGroupAndCheckItem(checkGroup,checkitemIds );
    }


    /**
     * 根据id删除检查组
     * @return
     */
    @Override
    public void deletegroup(Integer id) {
        checkgroupDao.deleteCheckGroupAndCheckItem(id);
        checkgroupDao.deletegroup(id);
    }

    /**
     * 查询所有的检查组
     * @return
     */
    @Override
    public List<CheckGroup> findall() {
       List<CheckGroup> checkGroupList= checkgroupDao.findall();
       return checkGroupList;
    }

    /**
     * 增加检查组
     * @return
     */
    @Override
    public void addgroup(CheckGroup checkGroup, Integer[] ids) {

        checkgroupDao.addgroup(checkGroup);
        setCheckGroupAndCheckItem(checkGroup,ids);

    }


    private void setCheckGroupAndCheckItem(CheckGroup checkGroup, Integer[] ids) {
        Integer checkgroup_id = checkGroup.getId();
        for (Integer checkitem_id : ids) {
            Map map = new HashMap();
            map.put("checkgroup_id", checkgroup_id);
            map.put("checkitem_id",checkitem_id );
            checkgroupDao.setCheckGroupAndCheckItem(map);
        }

    }
}
